package com.bclz.masterworker;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;

public class Master {
	
	//任务队列
	ConcurrentLinkedQueue<Task> taskQueue=new ConcurrentLinkedQueue<>();
	
	//结果
	ConcurrentHashMap<String, Object> context=new ConcurrentHashMap<>(1000);
	
	//线程
	Map<String, Thread> threadMap=new HashMap<>();
	
	
	
	public Map<String, Thread> getThreadMap() {
		return threadMap;
	}


	public void setThreadMap(Map<String, Thread> threadMap) {
		this.threadMap = threadMap;
	}


	public void submit(int count) {
		for(int i=0;i<count;i++) {
			Task t=new Task(new Random().nextInt(1000000)+"", "Task"+i);
			taskQueue.add(t);
		}
	}
	
	
	public void excute() {
		
		while(true) {
			
			new Work(this).start();
			
		}
		
	}
	
	public boolean isComplete() {
		
			
		for (Entry<String, Thread> th : threadMap.entrySet()) {
			
			if(th.getValue().getState()!=Thread.State.TERMINATED) {
				
				return false;
			}
			
		}
			
		return true;
	}
	
	
	public ConcurrentLinkedQueue<Task> getTaskQueue() {
		return taskQueue;
	}

	public void setTaskQueue(ConcurrentLinkedQueue<Task> taskQueue) {
		this.taskQueue = taskQueue;
	}

	public ConcurrentHashMap<String, Object> getContext() {
		return context;
	}

	public void setContext(ConcurrentHashMap<String, Object> context) {
		this.context = context;
	}
	
	
	
	
	
}
